package com.cangsan.erhai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.SysMenu;
import com.cangsan.erhai.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("menu_name",pageBean.getQuery());
        Page<SysMenu> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<SysMenu> pageResult = sysMenuService.page(page,queryWrapper);
        Map<String,Object> result = new HashMap<>();
        result.put("userList",pageResult.getRecords());
        result.put("total",pageResult.getTotal());
        return R.ok(result);
    }



    @GetMapping("/getList")
    public R getList(String query){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("menu_name","menu_id");
        queryWrapper.like("menu_name",query);
        return R.ok( sysMenuService.list(queryWrapper));
    }

    @PostMapping("/saveOrUpdate")
    public R add(@RequestBody SysMenu sysMenu){
        sysMenuService.saveOrUpdate(sysMenu);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R delete(@PathVariable Long id) {
        sysMenuService.removeById(id);
        return R.ok("删除成功");
    }


}
